CFLAGS = -g -O2 -Wall 
LDFLAGS = 
LIBS = 

SSL_CFLAGS = -DHAVE_SSL
SSL_LIBS = -lssl -lcrypto

ERLANG_CFLAGS = -I/usr/local/lib/erlang/lib/erl_interface-3.6.4/include -I/usr/local/lib/erlang/usr/include
ERLANG_LIBS = -L/usr/local/lib/erlang/lib/erl_interface-3.6.4/lib -lerl_interface -lei

DYNAMIC_LIB_CFLAGS = -fpic -shared


ERLSHLIBS = tls_drv.so


all:  $(ERLSHLIBS) tls.beam \
	test_client.beam test_server.beam test_ssl_client.beam test_ssl_server.beam \
	t_bio_client t_bio_server \
	t_bio_ssl_client t_bio_ssl_server 


$(ERLSHLIBS):	%.so:	%.c 
	gcc $(CFLAGS) $(LDFLAGS) $(INCLUDES) \
		$(subst ../,,$(subst .so,.c,$@)) $(LIBS) \
                $(SSL_LIBS) \
	        $(SSL_CFLAGS) \
		$(ERLANG_LIBS) \
		$(ERLANG_CFLAGS) \
		-o $@ \
		$(DYNAMIC_LIB_CFLAGS)

tls.beam: tls.erl
	erlc -o $@ $<

test_ssl_server.beam: test_ssl_server.erl
	erlc -o $@ $<

test_ssl_client.beam: test_ssl_client.erl
	erlc -o $@ $<

test_server.beam: test_server.erl
	erlc -o $@ $<

test_client.beam: test_client.erl
	erlc -o $@ $<

t_bio_client: t_bio_client.c
	gcc $(CFLAGS) $(LDFLAGS) $(INCLUDES) \
	$(SSL_LIBS) \
	$(SSL_CFLAGS) \
	-o $@ $<

t_bio_server: t_bio_server.c
	gcc $(CFLAGS) $(LDFLAGS) $(INCLUDES) \
	$(SSL_LIBS) \
	$(SSL_CFLAGS) \
	-o $@ $<

t_bio_ssl_client: t_bio_ssl_client.c
	gcc $(CFLAGS) $(LDFLAGS) $(INCLUDES) \
	$(SSL_LIBS) \
	$(SSL_CFLAGS) \
	-o $@ $<

t_bio_ssl_server: t_bio_ssl_server.c
	gcc $(CFLAGS) $(LDFLAGS) $(INCLUDES) \
	$(SSL_LIBS) \
	$(SSL_CFLAGS) \
	-o $@ $<

clean:
	rm t_bio_client t_bio_server t_bio_ssl_client t_bio_ssl_server
	rm -rfv *.beam
	rm -rfv *.so
	rm -rfv *~

